var newsletter = new liwe.module ( "newsletter", { 'unsubscribe' : '_unsubscribe', 'subscribe' : '_subscribe' } );

newsletter.cbacks = {	"form_extra" : null,
						"form_override" : null,
						"after_subscribe" : null,
						"after_unsubscribe": null
					};
newsletter._email = '';
newsletter._dest_div =  'block_main'; 

newsletter._init = function ()
{
	return;
};


//{{{HYSTORY
newsletter._subscribe = function ( params )
{
	var code = params.get ( 'code', '0' );
	var post = { 'action' : 'newsletter.ajax.subscribe_confirm'	, 'code' : code };
	
	newsletter.ajax ( post , function ( resp )
	{
		
		if ( newsletter.cbacks [ 'after_subscribe' ] )  newsletter.cbacks [ 'after_subscribe' ] ( newsletter.templates [ 'SITE_SUBSCRIBE_CONFIRM' ] );
	} );
	
};
newsletter._unsubscribe = function ( params )
{
	var id_subscriber = params.get ( 'id_subscriber' , 0 );
	var post = { 'action' : 'newsletter.ajax.unsubscribe_confirm', 'id_subscriber' : id_subscriber };
	newsletter.ajax ( post , function ( resp ) 
	{
		if ( newsletter.cbacks [ 'after_unsubscribe' ] )  newsletter.cbacks [ 'after_subscribe' ] ( newsletter.templates [ 'SITE_UNSUBSCRIBE_CONFIRM' ] );
	});
}
//}}}
//{{{newsletter.set_dest = function ( dest_div )
newsletter.set_dest = function ( dest_div )
{
	if ( typeof ( dest_div ) == 'undefined' ) return;
	newsletter._dest_div =  dest_div;
};
//}}}
//{{{ SUBSCRIPTION
newsletter.get_form = function ( btn_class,  cback )
{
	var _f  = new liwe.form.instance ( 'newsletter_box' );
	if (  btn_class  == null ) btn_class = "subscribe" ;
	user.ajax ( { 'action' : 'user.ajax.get_info' }, function ( _resp )
	{
		newsletter.ajax ( { 'action' : 'newsletter.ajax.get_subscriber' , 'email' : _resp.get ( 'email', '' ) }, function ( data )
		{
			if (  data.subscriber  == null  ) _sub = {}; else  _sub = data.subscriber;
			if ( newsletter.cbacks [ 'form_override' ] ) 
			{
				newsletter.cbacks [ 'form_override' ] ( _f, _resp , _sub );
				return;
			};
			newsletter._email = _resp.get ( 'email', '' );
			_f.hidden ( 'id_subscriber' , _sub.get ( 'id_subscriber' , 0 ) );
			_f.hidden ( 'uid' , _resp.get ( 'uid' , 0 ) );
			_f.sep();
			_f.descr ( { text: "Dati Iscrizione" } );
			_f.sep();
			_f.email ( { 'label' : 'e-mail', 'name' : 'email' , 'value' : _resp.get ( 'email', '' ), 'mandatory' : true , validator : true, onblur : newsletter._compile } );
			_f.text ( { 'label' : 'Nome', 'name' : 'name', 'mandatory' : true, size : 30, 'maxlength' : 30, 'value' : _sub.get ( 'name', '' ) } );
			_f.text ( { 'label' : 'Cognome', 'name' : 'lastname', 'mandatory' : true, size : 30, 'maxlength' : 30, 'value' : _sub.get ( 'lastname' , '' )  } );
			_f.text ( { 'label' : 'Localit&agrave;', 'name' : 'localita', size : 30, 'maxlength' : 30, 'value' : _sub.get ( 'localita' , '' )  } );
			_f.text ( { 'label' : 'Provincia', 'name' : 'provincia', size : 30, 'maxlength' : 30, 'value' : _sub.get ( 'provincia' )  } );
			_f.text ( { 'label' : 'Nazione', 'name' : 'nazione', size : 30, 'maxlength' : 30, 'value' : _sub.get ( 'nazione' , '' )  } );
			_f.text ( { 'label' : 'Azienda', 'name' : 'azienda', size : 30, 'maxlength' : 30, 'value' : _sub.get ( 'azienda' , '' )  } );
			_f.text ( { 'label' : 'Area', 'name' : 'area', size : 30, 'maxlength' : 30, 'value' : _sub.get ( 'area', '' )  } );
			_f.button ( { name : "subscribe", "value": "Iscriviti", onclick: newsletter.mail_subscribe, colspan : 2 } );
			var _free = 1;
			if ( _resp.get ( 'uid' ) )
			{
				_free = -1;
			};
			_f.sep();
			_f.descr ( { text: "Trattamento Dati personali" } );
			_f.sep();
			
			if ( newsletter.cbacks [ 'form_extra' ] ) newsletter.cbacks [ 'form_extra' ] ( _f, _resp );
			cback ( _f, _resp );
		} );
	} );
};

newsletter._compile = function ( f )
{
	var _email = f.get_value ( 'email' );
	if ( _email == newsletter._email ) return;
	newsletter.ajax ( { 'action' : 'newsletter.ajax.get_subscriber' , 'email' : _email }, function ( data )
	{
		if ( data.subscriber == null ) return;
		var _sub = data.subscriber;
		var _fields = [ 'id_subscriber', 'uid', 'name', 'lastname', 'localita', 'provincia', 'nazione', 'azienda', 'area' ];
		_fields.iterate ( function ( field ) 
		{
			f.set_value ( field, _sub [ field ] );
		} );
	} );

};

newsletter.mail_subscribe = function ()
{
	var _f = liwe.form.get ( "newsletter_box" );
	if ( ! _f.check () ) return;

    var privacy_form = $el ('privacy_form' );
    var privacy = "";
    for ( i = 0; i < privacy_form.privacy.length; ++i )
    {
        privacy = privacy_form.privacy [ i ].value;
        if ( privacy_form.privacy [ i ].checked ) break;
    };
	if ( privacy == "0" )
	{
		liwe.utils.notifier.show ( 'Per proseguire &egrave; necessario acconsentire al trattamento dei dati personali', 'info', 5000);
		return;
	};
	var _post =  _f.get_values();	
	_post [ 'action' ] = "newsletter.ajax.mail_subscribe";
	newsletter.ajax ( _post , function ( data )
	{
			$( newsletter._dest_div, String.formatDict ( newsletter.templates [ 'SITE_REGISTRATION_GREETING' ] , _post ) );
	});

};
//}}}
//{{{UNSUBSCRIBER
newsletter.mail_unsubscribe = function ( id_subscriber )
{
	post = { 'action' :  "newsletter.ajax.mail_unsubscribe", 'id_subscriber' : id_subscriber };
	newsletter.ajax ( post , function ( resp  )
	{
		$ ( newsletter._dest_div, newsletter.templates [ 'SITE_UNSUBSCRIBE_GREETING' ] ) ;
	});

};
//}}}
//{{{ RENDER
newsletter.render =  function ()
{
	var post = { 'action' : 'user.ajax.get_info' };
	user.ajax ( post , function ( data )
	{
		var uid = data.get ( 'uid', -1 );
		if ( uid != -1 )
		{
			var post = { 'action' : 'newsletter.ajax.get_subscriber', 'uid' : uid }; 
			newsletter.ajax ( post, function ( resp )
			{
				if ( resp.subscriber == null ) { newsletter._render_subscribe(); return; };
				$( newsletter._dest_div, String.formatDict ( newsletter.templates [ 'SITE_UNSUBSCRIBE_LAYOUT' ], resp.subscriber ) );
				return; 				
			} );
		} else {;
			newsletter._render_subscribe();
			return;
		}
	} );
};

newsletter._render_subscribe = function()
{
	$ ( newsletter._dest_div, newsletter.templates [ 'SITE_REGISTRATION_LAYOUT' ] )
	newsletter.get_form ( null , function ( f, uid )
	{
		f.set ( "newsletter_reg_form" );
		var button = f.get_element ( "subscribe" );
		var tr_button = button.parentNode.parentNode;
		tr_button.removeChild ( tr_button.firstChild );
		//var privacy = $el ( "privacy" );
		//privacy.innerHTML =  newsletter.templates [ 'SITE_REGISTRATION_PRIVACY' ] ; 
		$( 'newsletter_privacy' , newsletter.templates [ 'SITE_REGISTRATION_PRIVACY' ]  );

	} );
};
//}}}

//{{{newsletter.show_privacy = function ()
newsletter.show_privacy = function ()
{
	staticpage.get_page ( 'privacy_newsletter', true, function ( p ) 
	{
		liwe.lightbox.easy ( 'privacy_page', p [ 'title' ], 600, 600 );
		$( 'privacy_page', p [ 'html' ] );
	});
};
//}}}
//{{{newsletter.init = function ()
newsletter.init = function ()
{
	if ( ! newsletter.templates )
	{
		newsletter.load_templates ( function () 
		{
			newsletter._init ();
		} );
		return;
	}
};
//}}}

